import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import HomeView from '../views/Home.vue';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      show: true,
    },

    children: [
      {
        path: '',
        name: 'shouye',
        component: () =>
          import(/* webpackChunkName: "home" */ '../views/KaiShi.vue'),
      },
      {
        path: 'users',
        name: 'users',
        component: () =>
          import(/* webpackChunkName: "home" */ '../views/User.vue'),
      },
      {
        path: 'roles',
        name: 'roles',
        component: () =>
          import(/* webpackChunkName: "jiese" */ '../views/QuanXian.vue'),
      },
      {
        path: 'rights',
        name: 'rights',
        component: () =>
          import(
            /* webpackChunkName: "shangpin" */ '../views/zujian/quanxian/QuanXian.vue'
          ),
      },
      {
        path: 'goods',
        name: 'goods',
        component: () =>
          import(/* webpackChunkName: "shangpin" */ '../views/ShangPin.vue'),
      },
      {
        path: 'params',
        name: 'params',
        component: () =>
          import(
            /* webpackChunkName: "params" */ '../views/zujian/shangpin/Flcs.vue'
          ),
      },
      {
        path: 'categories',
        name: 'categories',
        component: () =>
          import(
            /* webpackChunkName: "categories" */ '../views/zujian/shangpin/Spfl.vue'
          ),
      },
      {
        path: 'orders',
        name: 'orders',
        component: () =>
          import(/* webpackChunkName: "dingdan" */ '../views/DingDan.vue'),
      },
      {
        path: 'reports',
        name: 'reports',
        component: () =>
          import(/* webpackChunkName: "tongji" */ '../views/TongJi.vue'),
      },
      {
        path: 'dingdan',
        name: 'dingdan',
        component: () =>
          import(
            /* webpackChunkName: "dingdan" */ '../views/zujian/shangpin/dingdan.vue'
          ),
      },
    ],
  },
  {
    path: '/denglu',
    name: 'denglu',
    component: () =>
      import(/* webpackChunkName: "home" */ '../views/Denglu.vue'),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});
//导航守护
router.beforeEach((to, from, next) => {
  if (to.meta.show) {
    const user_name = sessionStorage.getItem('token') || undefined;
    // console.log(to.meta);

    if (user_name) next();
    else next({ path: '/denglu', query: { redirect: to.path } as any });
  } else {
    next(); // next表示执行下一步。
  }
});

export default router;
